package driver.android.adapter;

import android.content.Context;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import driver.android.R;
import driver.util.Util;

public class UserInfoListAdapter extends ArrayAdapter {

    private List mUserTag = new ArrayList();
    private List mUserValue = new ArrayList();
    private LayoutInflater mLayoutInflater;
    private Context mContext;

    public UserInfoListAdapter(@NonNull Context context, int resource, String tasks) {
        super(context, resource);
        mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);
        parseContent(tasks);
    }

    private void parseContent(String userInfo) {
        try {
            mUserTag.add("Name");
            String firstName = Util.getJsonKeyValue("d_first_name", userInfo) == null ? "" : Util.getJsonKeyValue("d_first_name", userInfo);
            String lastName = Util.getJsonKeyValue("d_last_name", userInfo) == null ? "" : Util.getJsonKeyValue("d_last_name", userInfo);
            mUserValue.add(firstName + " " + lastName);
//            mUserTag.add("Earnings");
//            String earnings = Util.getJsonKeyValue("earnings", userInfo) == null ? "" : Util.getJsonKeyValue("earnings", userInfo);
//            mUserValue.add(" " + earnings);
            String detail = Util.getJsonKeyValue("summary", userInfo) == null ? "" : Util.getJsonKeyValue("summary", userInfo);
            JSONArray jsonTaskArray = new JSONArray(detail);
            for (int index = 0; index < jsonTaskArray.length(); index++) {
                String detailItem = jsonTaskArray.get(index).toString();
                JSONObject object = new JSONObject(detailItem);
                HashMap<String, String> item = new HashMap<String, String>();
                String key = object.getString("item");
                String value = object.getString("value");
                mUserTag.add(key);
                mUserValue.add(value);
            }
            mUserTag.add("App Version");
            mUserValue.add(getAppNameInfo());
        } catch (JSONException e) {
            e.printStackTrace();
            Util.debugApp('d', "解析用户信息异常");
        }
    }

    //    获取app版本信息
    private String getAppNameInfo() {
        try {
            //获取软件版本号，对应AndroidManifest.xml下android:versionCode
            String versionName = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionName;
            return versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "1.0.0";
    }

    @Override
    public int getCount() {
        return mUserTag.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Nullable
    @Override
    public String getItem(int position) {
        return (String) mUserTag.get(position);
    }

    @Override
    public int getPosition(@Nullable Object item) {
        return super.getPosition(item);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = this.mLayoutInflater.inflate(R.layout.f_user_info_item, parent, false);
            holder = new ViewHolder();
//            holder.userInfoIcon = (ImageView) convertView.findViewById(R.id.user_info_item_icon);
            holder.userInfoKey = (TextView) convertView.findViewById(R.id.user_info_item_key);
            holder.userInfoValue = (TextView) convertView.findViewById(R.id.user_info_item_value);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String itemTag = (String) mUserTag.get(position);
        String itemValue = (String) mUserValue.get(position);
        holder.userInfoKey.setText(itemTag);
        holder.userInfoValue.setText(itemValue);
        return convertView;
    }

    //static--只执行一次,而且先执行
    private static class ViewHolder {
//        ImageView userInfoIcon;
        TextView userInfoKey;
        TextView userInfoValue;
    }
}
